package yy.yyds.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yy.yyds.basic.query.util.QueryResult;
import yy.yyds.org.domain.Employee;
import yy.yyds.org.mapper.EmployeeMapper;
import yy.yyds.org.query.EmployeeQuery;
import yy.yyds.org.service.IEmployeeService;

import java.util.List;
@Service
/*读事务的注解*/
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class EmployeeServiceimpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    /*写事务*/
    @Transactional
    public void add(Employee employee) {
        employeeMapper.add(employee);
    }

    @Override
    /*写事务*/
    @Transactional
    public void delete(Long id) {
        employeeMapper.delete(id);

    }

    @Override
    /*写事务*/
    @Transactional
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public Employee loadOne(Long id) {
        return employeeMapper.loadOne(id);
    }

    @Override
    public List<Employee> loadAll() {
        return employeeMapper.loadAll();
    }

    @Override
    public QueryResult<Employee> querypage(EmployeeQuery employeeQuery) {
        /*查询总条数*/
        Integer total = employeeMapper.queryCount(employeeQuery);
        if (total == 0){//如果总条数已经是0条 就不需要后面的查询
//            ctrl + alt + 向右或向左 反复横跳
            return new QueryResult<>();
        }else {
            /*查询当前页数据*/
            List<Employee> departments = employeeMapper.queryList(employeeQuery);
            return new QueryResult<>(total,departments);
        }

    }

    @Override
    public void batchDelete(List<Long> ids) {
        employeeMapper.batchDelete(ids);
    }
}
